using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DTLAutomateGUI.Data;
using DTLAutomateGUI.DTLAutomate.Layout;

namespace DTLAutomateGUI.Controls.FormControls
{
	public partial class BlockPropertiesGroupBox : UserControl
	{
		public BlockPropertiesGroupBox()
		{
			InitializeComponent();
		}

		public BlockPropertiesGroupBox(DataSetLayout.BlockRow row)
		{
			InitializeComponent();
			this.DataRow = row;
		}

		private DataSetLayout.BlockRow dataRow;

		public DataSetLayout.BlockRow DataRow
		{
			get { return dataRow; }
			set 
			{ 
				dataRow = value;
				dataRow.Table.RowChanged += new DataRowChangeEventHandler(Table_RowChanged);
				switch (dataRow.type)
				{
					case "SimpleBlock":
						
						break;
					case "TurnoutBlock":
						this.iconLabel.Image = global::DTLAutomateGUI.Properties.Resources.TurnoutBlock;
						break;
					case "Turnout3to1Block":
						this.iconLabel.Image = global::DTLAutomateGUI.Properties.Resources.ThreewayBlock24;
						break;
					case "Turnout2to2Block":
						this.iconLabel.Image = global::DTLAutomateGUI.Properties.Resources.CrossoverBlock24;
						break;
					default:
						break;
				}
				this.repopulateFields();
			}
		}

		void Table_RowChanged(object sender, DataRowChangeEventArgs e)
		{
			if (e.Row == this.dataRow)
			{
				this.repopulateFields();
			}
		}

		private void repopulateFields()
		{
			this.idValueLabel.Text = dataRow.id.ToString();
			switchStateComboBox.Text = dataRow.switchState;
			nameTextBox.Text = dataRow.name;
			typeTextBox.Text = dataRow.type;
			isOccupiedComboBox.Text = dataRow.isOccupied.ToString();
			occupantIdTextBox.Text = dataRow.occupantId.ToString();
			reservedComboBox.Text = dataRow.reserved.ToString();
			ownerIdTextBox.Text = dataRow.ownerId.ToString();
			speedLimitNumericUpDown.Value = dataRow.goSpeed;
			slowSpeedNumericUpDown.Value = dataRow.slowSpeed;
			lengthTextBox.Text = dataRow.length.ToString();
			canStopComboBox.Text = dataRow.canStop.ToString();
			locationXTextBox.Text = dataRow.locationX.ToString();
			locationYTextBox.Text = dataRow.locationY.ToString();
		}

		private void nameTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.name = nameTextBox.Text;
			dataRow.AcceptChanges();
		}

		private void typeTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.type = typeTextBox.Text;
			dataRow.AcceptChanges();
		}

		private void speedLimitNumericUpDown_ValueChanged(object sender, EventArgs e)
		{
			dataRow.goSpeed = (int)speedLimitNumericUpDown.Value;
			dataRow.AcceptChanges();
		}

		private void slowSpeedNumericUpDown_ValueChanged(object sender, EventArgs e)
		{
			dataRow.slowSpeed = (int)slowSpeedNumericUpDown.Value;
			dataRow.AcceptChanges();
		}

		private void lengthTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.length = Int32.Parse(lengthTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void isOccupiedComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataRow.isOccupied = Boolean.Parse(isOccupiedComboBox.Text);
			dataRow.AcceptChanges();
		}

		private void occupantIdTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.occupantId = Int32.Parse(occupantIdTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void reservedComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataRow.reserved = Boolean.Parse(reservedComboBox.Text);
			dataRow.AcceptChanges();
		}

		private void ownerIdTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.ownerId = Int32.Parse(ownerIdTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void canStopComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataRow.canStop = Boolean.Parse(canStopComboBox.Text);
			dataRow.AcceptChanges();
		}

		private void locationXTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.locationX = Int32.Parse(locationXTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void locationYTextBox_TextChanged(object sender, EventArgs e)
		{
			dataRow.locationY = Int32.Parse(locationYTextBox.Text);
			dataRow.AcceptChanges();
		}

		private void switchStateComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			dataRow.switchState = switchStateComboBox.Text;
			dataRow.AcceptChanges();
		}

		private void BlockPropertiesGroupBox_Enter(object sender, EventArgs e)
		{
			this.nameTextBox.Focus();
		}

	}
}
